//
//  MNDNewFeatureView.m
//  minidai
//
//  Created by 谭谭伟 on 17/3/3.
//  Copyright © 2017年 谭谭伟. All rights reserved.
//

#import "MNDNewFeatureView.h"
#import <Masonry.h>
#import "UIColor+HMAddition.h"

//#import "MNDMainViewController.h"
#import "CustomPageControl.h"

@interface MNDNewFeatureView()<UIScrollViewDelegate>

/* 全局scrollView */
@property (strong, nonatomic) UIScrollView *featureScrollView;
/* 全局pageControl */
@property (strong, nonatomic) UIPageControl *pageControl;
@end

@implementation MNDNewFeatureView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self creatNewFeatureScrollView];
    }
    return self;
}

#pragma mark -重写图片数组set方法

- (void)setFeaturePictureArray:(NSArray *)featurePictureArray {
    _featurePictureArray = featurePictureArray;
    //设置初始frame
    CGRect rect = self.featureScrollView.bounds;
    
    //取出宽度
    CGFloat width = rect.size.width;
    
    //循环创建imageview
    for (int i = 0; i < _featurePictureArray.count; i++) {
        
        UIImageView *imgView = [[UIImageView alloc]init];
        
        //设置每张图片的frame
        imgView.frame = CGRectOffset(rect, i*width, 0);
        
        //设置图片
        UIImage *image = [UIImage imageNamed:_featurePictureArray[i]];
        imgView.image = image;
        
        //添加到scrollView
        [self.featureScrollView addSubview:imgView];
    }
    //图片数组拿到数据后设置scrollView的contentSize
    self.featureScrollView.contentSize = CGSizeMake(width*_featurePictureArray.count, rect.size.height);
    _pageControl.numberOfPages = self.featurePictureArray.count;

    //添加立即体验按钮
    [self addStartButton];
    
}

#pragma mark -创建scrollView

- (void)creatNewFeatureScrollView {
    /* scrollView */
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    //记录
    self.featureScrollView = scrollView;
    //开启分页
    scrollView.pagingEnabled = YES;
    //关闭滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    //禁止弹簧效果
    scrollView.bounces = NO;
    scrollView.delegate = self;
    
    /* 创建UIPageControl */
    CustomPageControl *pageControl = [[CustomPageControl alloc]init];
    pageControl.activeImage = [UIImage imageNamed:@"dot_HL"];
    pageControl.inactiveImage = [UIImage imageNamed:@"dot_Normal"];
    //禁用交互
    pageControl.userInteractionEnabled = NO;
    //设置num
    self.pageControl = pageControl;
    //当前页码
    pageControl.currentPage = 0;
//    UIImage *imgNormal = [UIImage imageNamed:@"dot_Normal"];
//    
//    UIImage *imgHighlight = [UIImage imageNamed:@"dot_HL"];
//    [pageControl setValue:imgNormal forKeyPath:@"_pageImage"];
//    
//    [pageControl setValue: imgHighlight forKeyPath:@"_currentPageImage"];
    //选择页颜色
//    pageControl.currentPageIndicatorTintColor = [UIColor hm_colorWithHex:0xff6300];
    //未选择页颜色
//    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    
    //添加到视图
    [self addSubview:scrollView];
    [self addSubview:pageControl];

    //布局
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(-60);
    }];
    

}

#pragma mark -添加体验按钮

- (void)addStartButton {
    
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //图片上有开始体验按钮
//    [startButton setTitle:@"开始体验" forState:UIControlStateNormal];
//    [startButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //计算位置
    CGFloat startButtonX = (_featurePictureArray.count - 1)*_featureScrollView.bounds.size.width+_featureScrollView.bounds.size.width*0.2;
    CGFloat startButtonY = _featureScrollView.bounds.size.height*0.85;
    
    //设置frame
    startButton.frame = CGRectMake(startButtonX, startButtonY, _featureScrollView.bounds.size.width*0.6, _featureScrollView.bounds.size.height*0.1);
    
    //添加点击事件
    [startButton addTarget:self action:@selector(clickStartButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加到父视图
    [self.featureScrollView addSubview:startButton];
    
}

#pragma mark -点击体验按钮

- (void)clickStartButton:(UIButton *)startButton {
    //发送通知展示内容
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showMinidaiNotification" object:nil userInfo:nil];
    
    //慢慢放大变透明
//    UIView *view = startButton.superview;
            //移除
            [self removeFromSuperview];

            //发送通知展示内容
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"showMinidaiNotification" object:nil userInfo:nil];
    //设置放大倍数
//    CGFloat scale = 3.0;
//    [UIView animateWithDuration:0.5 animations:^{
//        //放大
//        view.transform = CGAffineTransformMakeScale(scale, scale);
//        //透明
//        view.alpha = 0.5;
//    } completion:^(BOOL finished) {
//        //移除
//        [self removeFromSuperview];
//        
    
    
}

#pragma mark -UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //设置pagecontrol当前页码
    NSInteger currentPage = (scrollView.contentOffset.x/self.bounds.size.width) + 0.5;
    _pageControl.currentPage = currentPage;
    
    //到最后一页时隐藏
    _pageControl.hidden = currentPage == self.featurePictureArray.count-1;

}

@end
